package sushil.luc.msc;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.ugrokit.api.UgiTag;

import java.util.ArrayList;
import java.util.List;

import sushil.luc.gui.ScanTicketActivity;
import sushil.luc.item.Item;
import sushil.luc.item.ItemService;
import sushil.luc.item.ItemStatus;
import sushil.luc.ticket.Ticket;
import sushil.luc.truckersmartrfid.R;

public class TruckerCheckManager {

	private static Context con;
	//private Dialog dialog;
	private static String LogTag = "TruckerCheck";
	public static boolean initial;
	private static ArrayList<String> taglist;
	private static Ticket deliveryTicket;
	private static List<UgiTag> notTestAgain;
	private ItemService is;


	public TruckerCheckManager(Context con) {
		TruckerCheckManager.con = con;
		initial = true;
		taglist = null;
		notTestAgain = new ArrayList<UgiTag>();
		this.is = new ItemService();
	}
	/**
	 * Handles the current Tag.
	 * @param tag
	 */
	public void handleTag(UgiTag tag) {
        Log.d("TCM", tag.getEpc().toString());
		// check if we did not test it already and check if the dialog is not currently open
		if (!notTestAgain.contains(tag) && initial)
		{
			boolean test = this.checkTag(tag);
			Log.d("TCM", tag.getEpc().toString()+" inside "+test);
			if (!test)
			{
				initial = false;
				// get the item which was discovered but not in the Ticket
				getItemInfo(tag);
				//showDialog(tag);
			}
			else
			{
				setItemToStaged(tag);
			}
		}
	}
	/**
	 * Init the dialog and display it.
	 * @param tag
	 */
	public static void showDialog(Item wrongItem, final UgiTag tag) {
		// create a new dialog and assign the view xml file
		final Dialog dialog = new Dialog(con);
        dialog.setCancelable(false);
		dialog.setContentView(R.layout.dialog_trucker_check);
		dialog.setTitle("Item not in Ticket");

		TextView TagInfo = (TextView) dialog.findViewById(R.id.message);
		
		// tell trucker which item it was
		TagInfo.setText("Found Item "+wrongItem.getId()+", ItemName: "+wrongItem.getItemID());
		
		Button dialogRestartButton = (Button) dialog.findViewById(R.id.dialogButtonRestart);
		dialogRestartButton.setText(" Restart Scan ");
		// if button is clicked, close the custom dialog
		dialogRestartButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d(LogTag, "Restart Button");
				// set all items back to ischecked. Trucker has to scan all items asgain
				resetAllItems();
				
				reset();
				dialog.dismiss();
			}
		});

		dialog.show();
	}
	


	/**
	 * reset variables to intial state.
	 */
	public static void reset() {
		initial = true;
		//taglist = new ArrayList<String>();
		notTestAgain = new ArrayList<UgiTag>();
	}
	
	/**
	 * add all the rfid tag of every item into a list to provide a faster check
	 */
	private void initList() {
		if (deliveryTicket!=null)
		{
            taglist = new ArrayList<String>();
            List<Item> tmp = deliveryTicket.getItems();
			for (Item i :tmp)
			{
				taglist.add(i.getRFID());
			}
		}

	}
	
	/**
	 * Check if the current scanned item is in the list
	 * @param tag
	 * @return true if the tag is in the ticket. False if not
	 */
	private boolean checkTag(UgiTag tag) {
		String foundRFID = tag.getEpc().toString();
		
		notTestAgain.add(tag);
		
		if (taglist.contains(foundRFID))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * init the Ticket
	 * @param t
	 */
	public void setTicket(Ticket t) {
		deliveryTicket = t;
		initList();
	}
	
	/**
	 * Get some infos of the tag. Ask the Server 
	 * @param tag
	 * @return an Item object with the corresponding tag
	 */
	private Item getItemInfo (UgiTag tag)
	{		
		return is.fetchItemFromRfid(tag);
	}
	
	/**
	 *  resets all the Items of the ticket to checked
	 */
	private static void resetAllItems ()
	{
		for (Item i : deliveryTicket.getItems())
		{
			i.setStatus(ItemStatus.Checked);
		}
		deliveryTicket.calcTicketStatus();
        ScanTicketActivity.fillItemList();
		
	}
	
	/**
	 * Set a specific item to staged
	 * @param tag
	 */
	private void setItemToStaged (UgiTag tag)
	{
		for (Item i : deliveryTicket.getItems())
		{
			if (i.getRFID().equals(tag.getEpc().toString()))
				i.setStatus(ItemStatus.Staged);
		}
		deliveryTicket.calcTicketStatus();
        ScanTicketActivity.fillItemList();
		
		//TODO implement this function. Should be done at the end when we leave the activity
		//ScanTicketActivity.updateItemList();
		
	}


}
